'use strict';

const Service = require('egg').Service;
const path = require('path');
const mkdirp = require('mkdirp');
const moment = require('moment');

class UploadService extends Service {
  // 获取上传的目录
  async getUploadFile(filename) {
    // 把当前时间作为上传后的文件名
    const dirName = moment().format('YYYYMMDD');
    // 获取图片存储路径
    const dirPath = path.join(this.config.uploadDir, dirName);
    // 不存在则创建目录
    await mkdirp(dirPath);
    const now = +new Date();
    // 返回文件保存路径
    const uploadDir = path.join(dirPath, now + path.extname(filename));
    return {
      uploadDir,
      saveDir: uploadDir.slice(3).replace(/\\/g, '/'),
    };
  }
}

module.exports = UploadService;
